// 价格调整测试类
@isTest
private class PriceAdjustControllerTest {

	//子公司调整价格体系
    static testMethod void testMethod1() {
      	

    	ProductStructure__c ps = new ProductStructure__c();
        ps.Classify3__c = '试剂';
        insert ps;

        Id productType = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        Product__c p1 = TestUtils.createProduct();
        p1.Category0__c = '试剂';
        p1.ProductStructure__c = ps.Id;
        p1.PriceCompany__c = 'MKSY';
        p1.NCCode__c = '000001';
        p1.RecordTypeId = productType;
        
        Product__c p2 = TestUtils.createProduct();
        p2.Category0__c = '试剂';
        p2.ProductStructure__c = ps.Id;
        p2.PriceCompany__c = 'MKSY';
        p2.NCCode__c = '000002';
        p2.RecordTypeId = productType;
        p2.GroupRegionalDistributorsPrice__c = 10;
        insert new Product__c[] {p1, p2};
  
        Price__c pr1 = new Price__c(
            Product__c = p1.Id,
            Company__c = '四川省迈克实业有限公司',
            CompanyBottomPrice__c = 1.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 1.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 0.5,
            Province__c = '四川省/西藏'
            
        );
        Price__c pr2 = new Price__c(
            Product__c = p2.Id,
            CompanyBottomPrice__c = 2.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 2.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,

            InstCost__c = 0.5
            
        );
        insert new Price__c[] {pr1,pr2};


        PriceAdjustment__c pa = new PriceAdjustment__c();
        pa.Company__c = '四川省迈克实业有限公司';
        pa.Province__c = '四川省/西藏';
       	pa.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('PriceAdjustmentSub', 'PriceAdjustment__c');
       	insert pa;

       	PriceAdjustmentDetail__c pd = new PriceAdjustmentDetail__c();
       	pd.Product__c = p1.Id;
       	pd.NewAreaDealerPrice__c = 10;
       	pd.PriceAdjustment__c = pa.Id;
       	pd.AdjustmentType__c = '更新';
       	insert pd;


       	System.debug(pa.Id);
       	PageReference page1 = new PageReference('/apex/PriceAdjust?id='+pa.Id);
       	System.Test.setCurrentPage(page1);


       	PriceAdjustController pac = new PriceAdjustController();
       	pac.setNil();
       	pac.init();

       	//pac.getDetails();

       	Map<String,String> searchMap = new Map<String,String>();
       	searchMap.put('NCCode__c','000002');
       	pac.searchJson = EncodingUtil.urlEncode(JSON.serialize(searchMap), 'UTF-8');
       	pac.search();

       	pac.getDetails();
       	pac.saveJson = pac.dataJson;

       	pac.save();

       	pa.Status__c = '已批准';
       	update pa;

       	//pac.getNoPriceProduct();

       
        //集团统一调价
        pa.Status__c  = '审批中';
        pa.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('AdjustAll', 'PriceAdjustment__c');
        pa.CompanyDup__c = '四川省迈克实业有限公司';
        update pa;
        System.Test.startTest();
        pa.Status__c = '已批准';
        update pa;
       	

    }



    //集团调整价格体系
    static testMethod void testMethod2() {
      	

    	ProductStructure__c ps = new ProductStructure__c();
        ps.Classify3__c = '试剂';
        insert ps;

        Id productType = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        Product__c p1 = TestUtils.createProduct();
        p1.Category0__c = '试剂';
        p1.ProductStructure__c = ps.Id;
        p1.PriceCompany__c = 'MKSY';
        p1.NCCode__c = '000001';
        p1.RecordTypeId = productType;
        
        Product__c p2 = TestUtils.createProduct();
        p2.Category0__c = '试剂';
        p2.ProductStructure__c = ps.Id;
        p2.PriceCompany__c = 'MKSY';
        p2.NCCode__c = '000002';
        p2.RecordTypeId = productType;
        p2.GroupRegionalDistributorsPrice__c = 10;
        insert new Product__c[] {p1, p2};
  
        Price__c pr1 = new Price__c(
            Product__c = p1.Id,
            Company__c = '四川省迈克实业有限公司',
            CompanyBottomPrice__c = 1.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 1.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 0.5,
            Province__c = '四川省/西藏'
            
        );
        Price__c pr2 = new Price__c(
            Product__c = p2.Id,
            CompanyBottomPrice__c = 2.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 2.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,

            InstCost__c = 0.5
            
        );
        insert new Price__c[] {pr1,pr2};


        PriceAdjustment__c pa = new PriceAdjustment__c();
        pa.Company__c = '四川省迈克实业有限公司';
        pa.Province__c = '四川省/西藏';
       	pa.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('PriceAdjustmentHead', 'PriceAdjustment__c');
       	insert pa;

       	PriceAdjustmentDetail__c pd = new PriceAdjustmentDetail__c();
       	pd.Product__c = p1.Id;
       	pd.NewAreaDealerPrice__c = 10;
       	pd.PriceAdjustment__c = pa.Id;
       	pd.AdjustmentType__c = '更新';
       	insert pd;


       	System.debug(pa.Id);
       	PageReference page1 = new PageReference('/apex/PriceAdjust?id='+pa.Id);
       	System.Test.setCurrentPage(page1);


       	PriceAdjustController pac = new PriceAdjustController();
       	pac.setNil();
       	pac.init();
       	Map<String,String> searchMap = new Map<String,String>();
       	searchMap.put('NCCode__c','000002');
       	pac.searchJson = EncodingUtil.urlEncode(JSON.serialize(searchMap), 'UTF-8');
       	pac.search();


       	pac.getDetails();
       	pac.saveJson = pac.dataJson;

       	pac.save();

       	pa.Status__c = '已批准';
       	update pa;
       	
       	//pac.getNoPriceProduct();
       	

    }


    //海外整价格体系
    static testMethod void testMethod3() {
      	

    	ProductStructure__c ps = new ProductStructure__c();
        ps.Classify3__c = '试剂';
        insert ps;

        Id productType = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
       


        Id  ldcProfile = [select Id from Profile where Name like '%海外销售助理%' limit  1].Id ;
         User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='迈克生物股份有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Username = 'Matldc1@test.com.maike',
                         Province__c = '四川省',
                         CompanyName = 'Y-MKSW',
                         ManagerId = UserInfo.getUserId(),
                         FederationIdentifier = '123451',
                         ResponseCode__c = 'TTTT' );
    	 insert u1;

    	 System.runAs(u1){

    	 	 Product__c p1 = TestUtils.createProduct();
        p1.Category0__c = '试剂';
        p1.ProductStructure__c = ps.Id;
        p1.Brand__c = 'maccura';
        p1.Overseasproducts__c = true;
        p1.PriceCompany__c = 'MKSW';
        p1.NCCode__c = '000001';
        p1.RecordTypeId = productType;
        
        Product__c p2 = TestUtils.createProduct();
        p2.Category0__c = '试剂';
        p2.ProductStructure__c = ps.Id;
        p2.Brand__c = 'maccura';
        p2.Overseasproducts__c = true;
        p2.PriceCompany__c = 'MKSW';
        p2.NCCode__c = '000002';

        p2.RecordTypeId = productType;
        p2.GroupRegionalDistributorsPrice__c = 10;
        insert new Product__c[] {p1, p2};
  
        Price__c pr1 = new Price__c(
            Product__c = p1.Id,
            Company__c = '迈克生物股份有限公司',
            CompanyBottomPrice__c = 1.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 1.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 0.5,
            Province__c = '海外'
            
        );
        Price__c pr2 = new Price__c(
            Product__c = p2.Id,
            CompanyBottomPrice__c = 2.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 2.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,

            InstCost__c = 0.5
            
        );
        insert new Price__c[] {pr1,pr2};

    	 	 PriceAdjustment__c pa = new PriceAdjustment__c();
	        pa.Company__c = '迈克生物股份有限公司';
	        pa.Province__c = '海外';
	       	pa.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('PriceAdjustmentOversea', 'PriceAdjustment__c');
	       	insert pa;

	       	PriceAdjustmentDetail__c pd = new PriceAdjustmentDetail__c();
	       	pd.Product__c = p1.Id;
	       	pd.NewAreaDealerPrice__c = 10;
	       	pd.PriceAdjustment__c = pa.Id;
	       	pd.AdjustmentType__c = '更新';
	       	insert pd;


	       	System.debug(pa.Id);
	       	PageReference page1 = new PageReference('/apex/PriceAdjust?id='+pa.Id);
	       	System.Test.setCurrentPage(page1);


	       	PriceAdjustController pac = new PriceAdjustController();
	       	pac.setNil();
	       	pac.init();
	       	Map<String,String> searchMap = new Map<String,String>();
	       	searchMap.put('NCCode__c','000002');
	       	pac.searchJson = EncodingUtil.urlEncode(JSON.serialize(searchMap), 'UTF-8');
	       	pac.search();


	       	pac.getDetails();
	       	pac.saveJson = pac.dataJson;

	       	pac.save();

	       	pa.Status__c = '已批准';
	       	update pa;

    	 	
    	 }
       
       	
       	//pac.getNoPriceProduct();
       	

    }
}